/*
' Copyright (c) 2011  DevPCI
'  All rights reserved.
' 
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
' DEALINGS IN THE SOFTWARE.
' 
*/

using System;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Services.Localization;
using DotNetNuke.Security;
using DotNetNuke.Entities.Tabs;
using System.Collections.Generic;


namespace DotNetNuke.UI.Skins.Controls.DDT_LastContentModifiedOnDate
{

    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The View DDT_LastContentModifiedOnDate class displays the content
    /// </summary>
    /// -----------------------------------------------------------------------------
    public partial class DDT_LastContentModifiedOnDate : DDT_LastContentModifiedOnDateModuleBaseSO
    {

        #region Event Handlers

        override protected void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }

        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);
        }


        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Page_Load runs when the control is loaded
        /// </summary>
        /// -----------------------------------------------------------------------------
        private void Page_Load(object sender, System.EventArgs e)
        {
            try
            {
                //Code using the LastContentModifiedOnDate property of the Modules table (available since DNN 6.0) for the Skin Object component

                // Prepare variables to list all the modules in the active tab :
                DotNetNuke.Entities.Modules.ModuleController ctlModule = new DotNetNuke.Entities.Modules.ModuleController();
                List<DateTime> dates = new List<DateTime>();
                TabController objTabController = new TabController();
                TabInfo objTabInfo = objTabController.GetTab(PortalSettings.ActiveTab.TabID, PortalSettings.PortalId, true);
                ModuleController objModuleController = new ModuleController();                
                
                // * if we need the last modified information of the active tab, we could use : LastModifiedOnDate (date of the last update settings) instead of LastContentModifiedOnDate
                // dates.Add(objTabInfo.LastModifiedOnDate);

                // Create the list (dictionary) that will contains all the modules of the active tab :
                Dictionary<int, ModuleInfo> lstModuleInfo = default(Dictionary<int, ModuleInfo>);
                lstModuleInfo = objModuleController.GetTabModules(PortalSettings.ActiveTab.TabID);
                ModuleInfo objModuleInfo = default(ModuleInfo);
                
                //Loop on all the tab's modules list to find all the last content modified dates realeased :
                foreach (var m in lstModuleInfo)
                {
                    objModuleInfo = m.Value;
                    if ((objModuleInfo.IsDeleted == false) & (objModuleInfo.StartDate <= DateTime.Now) & (objModuleInfo.EndDate <= DateTime.Now))
                    {
                        objModuleController.GetModuleSettings(objModuleInfo.ModuleID);
                        // * Same as comment line 66 for the Settings Module last update date information, we could use : 
                        // dates.Add(objModuleInfo.LastModifiedOnDate);
                        dates.Add(objModuleInfo.LastContentModifiedOnDate);
                    }
                }
                //order dates
                dates.Sort();

                // get the more recent date :
                DateTime lastDate = dates.FindLast(d => d < DateTime.Now);
                
                
                //send it to the caption of the label
                LblDate.Text = lastDate.ToString();

                // Manage Multilanguage resources for the Skin Object Component
                LblUpdate.Text = Services.Localization.Localization.GetString("LblUpdate", Services.Localization.Localization.GetResourceFile(this, "DDT_LastContentModifiedOnDate.ascx"));


            }
            catch (Exception exc) //Module failed to load
            {
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        #endregion

    }

}
